package com.xinchuang.service.actual.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xinchuang.comment.KeyWord;
import com.xinchuang.comment.R;
import com.xinchuang.domain.entity.TestModelParam;
import com.xinchuang.entity.api.MouldInfoVo;
import com.xinchuang.manager.TestModelParamService;
import com.xinchuang.service.actual.SynService;
import com.xinchuang.utils.MyUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2023/11/8 9:08
 */
@Service
@RequiredArgsConstructor
public class SynServiceImpl implements SynService {

    private final KeyWord keyWord;

    private final String SYN_URL = "/api/Nem/Mould/MouldInfos";

    private final TestModelParamService testModelParamService;

    @Override
    public R synMold(JSONObject json) {
        String url = keyWord.getApi() + SYN_URL;
//        String time = json.getString("time");
//        if (StringUtils.isNotBlank(time) && !MyUtils.isISO8601(time)) {
//            return R.fail(500,"时间格式不正确，请确认后重试");
//        } else if (StringUtils.isNotBlank(time)) {
//            url += "?earliestCreateTime=" + time;
//        }
        HttpResponse httpResponse = HttpUtil
                .createRequest(Method.GET,  url)
                .timeout(5 * 60 * 1000)
                .execute();
        if (httpResponse.getStatus() == HttpStatus.HTTP_OK) {
            JSONArray array = JSONArray.parseArray(httpResponse.body());
            if (CollectionUtil.isNotEmpty(array)) {
                List<String> list = new ArrayList<>();
                List<MouldInfoVo> mouldInfoVos = array.toJavaList(MouldInfoVo.class);
                for (MouldInfoVo mouldInfoVo : mouldInfoVos) {
                    TestModelParam one = testModelParamService.getOne(new LambdaUpdateWrapper<TestModelParam>()
                            .eq(TestModelParam::getMoldId, mouldInfoVo.getMouldNumber()).last("LIMIT 1"));
                    if (ObjectUtil.isEmpty(one)) {
                        one = new TestModelParam();
                        one.setMoldId(mouldInfoVo.getMouldNumber());
                        one.setMoldName(mouldInfoVo.getProductName());
                        one.setProductName(mouldInfoVo.getProductName());
                        one.setProjectName(mouldInfoVo.getProjectName());
                        one.setProjectManager(mouldInfoVo.getProjectEngineerName());
                        one.setTongmanName(mouldInfoVo.getFitterTeamName());
                        testModelParamService.save(one);
                    } else {
                        list.add(mouldInfoVo.getMouldNumber());
                    }
                }
                if (CollectionUtil.isNotEmpty(list)) {
                    StringBuffer buffer = new StringBuffer();
                    for (String s : list) {
                        buffer.append(s + ";");
                    }
                    return R.success("同步成功,有" + list.size() + "个重复的模具,分别为:" + buffer.toString());
                } else {
                    return R.success("同步成功");
                }
            } else {
                return R.success("暂不需要更新");
            }
        } else {
            return R.fail("请求异常,请稍后重试!");
        }
    }
}
